package com.yueyang.sojourn.server.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary 手机号工具类
 * @since 2018/11/28
 */
@Slf4j
public class PhoneNumberUtil {

    /**
     * 判断是否手机号码
     * @param phone
     * @return
     */
    public static boolean isPhoneNumber(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }

    /**
     * 获取加密中间四位的手机号
     * @param phone
     * @return
     */
    public static String getSecPhone(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return "";
        }
        if (isPhoneNumber(phone)) {
            return phone.substring(0, 3) + "****" + phone.substring(7, 11);
        }
        log.warn("微信手机号格式异常:{}", phone);
        return phone;
    }
}
